const Base = require('./base')
module.exports = class extends Base {
  async __before () {
    const apiServ = this.service('api')
    const id = this.get('id')
    if (!id) return this.fail(-1, '参数不能为空')
    this.api = await apiServ.instance(id)
    if (!this.api) return this.fail('-1,您已经取消授权,请重新授权再试!')
  }

  /**
 * @api {get} /wechat/api/jssdk 微信jssdk
 * @apiDescription 微信jssdk
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {String} url 当前url
 * @apiParam {Boolean} debug 调试模式
 * @apiSuccess {Object} data jssdk配置信息
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async jssdkAction () {
    const param = this.get()
    this.success(await this.api.ticket.get_js(param))
  }

  /**
 * @api {get} /wechat/api/jscard 微信卡券jssdk
 * @apiDescription 微信卡券jssdk
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {Number} card_id 卡券id
 * @apiParam {String} code code
 * @apiParam {String} openid 会员openid
 * @apiParam {String} balance 卡券balance
 * @apiSuccess {Object} data jssdk配置信息
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async jscardAction () {
    const param = this.get()
    this.success(await this.api.ticket.get_card(param))
  }

  /**
 * @api {post} /wechat/api/postmenu 创建微信菜单
 * @apiDescription 创建微信菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {Object} postData 菜单格式信息
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async postmenuAction () {
    this.this.success(await this.api.create_menu(this.post()))
  }

  /**
 * @api {get} /wechat/api/getmenu 获取微信菜单
 * @apiDescription 获取微信菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async getmenuAction () {
    this.this.success(await this.api.get_menu())
  }

  /**
 * @api {get} /wechat/api/removemenu 删除微信菜单
 * @apiDescription 删除微信菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async removemenuAction () {
    this.this.success(await this.api.remove_menu())
  }

  /**
 * @api {get} /wechat/api/getcusmenu 获取自定义菜单
 * @apiDescription 获取自定义菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async getcusmenuAction () {
    const d = await this.api.get_custom_menu()
    this.success(d)
  }

  /**
 * @api {post} /wechat/api/postcusmenu 创建自定义菜单
 * @apiDescription 创建自定义菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {Object} postData 菜单格式信息
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async postcusmenuAction () {
    const post = this.post()
    this.this.success(await this.api.create_custom_menu(post))
  }

  /**
 * @api {post} /wechat/api/removecusmenu 删除自定义菜单
 * @apiDescription 删除自定义菜单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {Object} postData 菜单格式信息
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async removecusmenuAction () {
    const post = this.post()
    this.this.success(await this.api.remove_custom_menu(post))
  }

  /**
 * @api {get} /wechat/api/limitqrcode 永久带参数二维码
 * @apiDescription 永久带参数二维码
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {String} sid 二维码sence_id
 * @apiParam {Boolean} show_img 是否显示二维码
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async limitqrcodeAction () {
    const sid = this.get('sid')
    const showImg = this.get('show_img') || false
    if (!sid) this.error('缺少参数')
    //
    const d = await this.api.limit_qrcode(sid)
    const img = this.api.show_qrcode(d.ticket)
    //
    if (showImg) return this.body = `<img src="${img}">`
    this.success(img)
  }
  /**
 * @api {get} /wechat/api/tmpqrcode 临时带参数二维码
 * @apiDescription 临时带参数二维码
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {String} sid 二维码sence_id
 * @apiParam {Boolean} show_img 是否显示二维码
 * @apiParam {Number} expire 失效时间
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async tmpqrcodeAction () {
    const sid = this.get('sid')
    const showImg = this.get('show_img') || false
    const expire = this.get('expire') || 604800
    if (!sid) this.error('缺少参数')
    //
    const d = await this.api.tmp_qrcode(sid, expire)
    const img = this.api.show_qrcode(d.ticket)
    if (showImg) return this.body = `<img src="${img}">`
    this.success(img)
  }

  /**
 * @api {get} /wechat/api/ips 微信服务器白名单
 * @apiDescription 微信服务器白名单
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async ipsAction () {
    this.success(await this.api.get_ip())
  }

  /**
 * @api {get} /wechat/api/shorturl 生成短连接
 * @apiDescription 生成短连接
 * @apiVersion 1.0.0
 * @apiGroup wechat_api
 * @apiPermission admin
 * @apiParam {Number} id 公众号id
 * @apiParam {String} sid 二维码sence_id
 * @apiParam {Boolean} show_img 是否显示二维码
 * @apiParam {Number} expire 失效时间
 * @apiSuccess {Object} data 返回值
 * @apiSuccess {Number} code=0 返回码
 * @apiSuccess {String} msg 返回信息
 */
  async shorturlAction () {
    const url = this.get('url') || false
    if (!url) return this.error('缺少参数')
    this.success(await this.api.short_url(url))
  }
}
